package com.cote.douhahou.pretloan.beans;

import android.text.TextUtils;

import androidx.fragment.app.FragmentActivity;

import com.cote.douhahou.pretloan.launcher.BaseContext;
import com.cote.douhahou.pretloan.network.HttpHelper;
import com.google.gson.Gson;
import com.cote.douhahou.pretloan.R;


public class PersAkomapalendonalInfo {

    private static class Inner {
        private static PersAkomapalendonalInfo instance = new PersAkomapalendonalInfo();
    }

    private PersAkomapalendonalInfo(){}

    public static PersAkomapalendonalInfo instance(){
        return Inner.instance;
    }

    public static void parse_akomapalend(String userInfo){
        if (!TextUtils.isEmpty(userInfo)) Inner.instance = new Gson().fromJson(userInfo, PersAkomapalendonalInfo.class);
        else {
            Inner.instance = new PersAkomapalendonalInfo();
            Inner.instance.uploadUserInfo(null,null);
        }
    }

    public void uploadUserInfo(FragmentActivity activity,Runnable responseRunnable){
        String data = new Gson().toJson(this);

        if (TextUtils.isEmpty(userAvatar)) userAvatar = "";
        if (expValue<10) expValue = 10;
        else if (expValue>300) expValue = 300;
        if (TextUtils.isEmpty(fullName)) fullName = BaseContext.instance().getString(R.string.personal_no_name);
        if (TextUtils.isEmpty(userGender)) userGender = BaseContext.instance().getString(R.string.personal_gender_unknown);

        if (activity!=null && responseRunnable!=null)
            HttpHelper.inst().saveUserData(data,activity,responseRunnable);
        else
            HttpHelper.inst().saveUserData(data);
    }

    private String userAvatar = "";//头像（图片链接）
    private int expValue = 10;//经验值
    private String fullName = "";//用户全名
    private String userGender = "";//用户性别

    public void setUserAvatar(String userAvatar) {
        this.userAvatar = userAvatar;
    }

    public void addExpValue(int increaseExp) {
        if (expValue<10) expValue = 10;
        else if (expValue>300) expValue = 300;

        this.expValue += increaseExp;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public void setUserGender(String userGender) {
        this.userGender = userGender;
    }

    public String getUserAvatar() {
        if (TextUtils.isEmpty(userAvatar)) return "";
        return userAvatar;
    }

    public int getExpValue() {
        return expValue;
    }

    public String getFullName() {
        if (TextUtils.isEmpty(fullName)) return "";
        return fullName;
    }

    public String getUserGender() {
        if (TextUtils.isEmpty(userGender)) return "";
        return userGender;
    }

    public int getGrade(){

        if (expValue>=300) return 3;
        else if (expValue>=100) return 2;

        return 1;
    }

    public int getNextGradeExp(){

        if (expValue<10) expValue = 10;
        else if (expValue>300) expValue = 300;

        if (expValue<100) return 100-expValue;
        else if (expValue<300) return 300-expValue;
        else return 0;

    }

}
